package com.atguigu.controller;

import com.atguigu.entity.UserInfo;
import com.atguigu.result.Result;
import com.atguigu.result.ResultCodeEnum;
import com.atguigu.service.UserInfoService;
import com.atguigu.util.MD5;
import com.atguigu.vo.LoginVo;
import com.atguigu.vo.RegisterVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Controller;
import org.springframework.util.StreamUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * @title：
 * @author：Yu
 * @create：2023-02-24 10:14
 * @description：
 */
@ResponseBody
@Controller
@RequestMapping("/userInfo")
public class UserInfoController {
    @DubboReference
    private UserInfoService userInfoService;

    @RequestMapping("/sendCode/{phone}")
    public Result sendCode(@PathVariable String phone, HttpSession session) {
        //真实环境：随机生成一个4或6位的验证码，将这个数字发送到用户手机上
        //模拟环境：随机生成一个4或6位的验证码，将这个数字返给前端
        String code = "8888";
        //由于前台还要发送请求验证，因此我们将这个验证码保存到sessin中，key用手机号即可
        session.setAttribute(phone, code);
        return Result.ok(code);
    }

    @RequestMapping("/register")
    public Result register(@RequestBody RegisterVo registerVo, HttpSession session) {
        //1.从registerVo中获取用户后台输入的表单信息
        String phone = registerVo.getPhone();
        String password = registerVo.getPassword();
        String code = registerVo.getCode();
        String nickName = registerVo.getNickName();
        //2.判断表单是否为null或空
        if (StringUtils.isEmpty(password) || StringUtils.isEmpty(phone) || StringUtils.isEmpty(code) || StringUtils.isEmpty(nickName)) {
            return Result.build(null, ResultCodeEnum.PARAM_ERROR);
        }
        //3.验证手机号是否存在:如果已存在则改手机号不可以注册
        UserInfo userInfo = userInfoService.getByPhone(phone);
        if (userInfo != null) {
            return Result.build(null, ResultCodeEnum.PHONE_REGISTER_ERROR);
        }
        //4.判断验证码是否正确
        String oriCode = (String) session.getAttribute(phone);
        if (oriCode != null) {
            if (!code.equals(oriCode)) {
                return Result.build(null, ResultCodeEnum.CODE_ERROR);
            }
        } else {
            return Result.build(null, ResultCodeEnum.ACCOUNT_ERROR);
        }
        //5.数据添加
        UserInfo uf = new UserInfo();
        uf.setPhone(phone);
        uf.setPassword(MD5.encrypt(password));
        uf.setNickName(nickName);
        userInfoService.insert(uf);
        return Result.ok();
    }

    @RequestMapping("/login")
    public Result login(@RequestBody LoginVo loginVo, HttpSession session) {
        //1.获取前台的数据
        String phone = loginVo.getPhone();
        String password = loginVo.getPassword();
        //2.进行非空验证
        if (StringUtils.isEmpty(password) || StringUtils.isEmpty(phone)) {
            return Result.build(null, ResultCodeEnum.PARAM_ERROR);
        }
        //3.手机号验证：验证手机号是否在数据库中保存
        UserInfo userInfo = userInfoService.getByPhone(phone);
        if (userInfo == null) {
            return Result.build(null, ResultCodeEnum.ACCOUNT_ERROR);
        }
        //4.验证该正好的密码是否正确
        if (!userInfo.getPassword().equals(MD5.encrypt(password))) {
            return Result.build(null, ResultCodeEnum.PASSWORD_ERROR);
        }
        //5.判断用户是否被锁定
        if (userInfo.getStatus() != 1) {
            return Result.build(null, ResultCodeEnum.ACCOUNT_LOCK_ERROR);
        }
        //6.返回手机号以及昵称，是为了前台将用户的数据存储带客户端本地,如欢迎XXX
        Map map = new HashMap<>();
        map.put("phone", phone);
        map.put("nickName", userInfo.getNickName());
        //7.将当前登陆人对象存储在session内（当前会话有效）
        session.setAttribute("userInfo", userInfo);
        return Result.ok(map);
    }

    @RequestMapping("/logout")
    public Result logout(HttpSession session) {
        session.removeAttribute("userInfo");
        return Result.ok();
    }
}
